<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();


jimport( 'joomla.application.component.model' );
class NvModelPropinsi extends JModel{

	//----------------------  First Initialize of variable
	var $_id = null;
	var $_data = null;


	function __construct(){
		parent::__construct();
		//---------------------- get a value of edit
		$array = JRequest::getVar('cid', array(0), '', 'array');
		//----------------------  pass it into function
		$this->setId((int)$array[0]);

		//----------------------  an instance of JApplication
		global $mainframe;

		//---------------------- setting up default of paging
		$limit = $mainframe->getUserStateFromRequest( 'limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart = $mainframe->getUserStateFromRequest( 'limitstart', 'limitstart', 0, 'int' );
		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);
	}

	function setId($propinsi_id){
		//---------------------- set id as global variable in this class
		$this->_id		= $propinsi_id;
		$this->_data	= null;
	}

	// function to get propinsi data either if it's new or edit
	function getData(){
		global $mainframe,$option;
		$search	= $mainframe->getUserStateFromRequest( $option.'search','search','','string' );
		$search	= JString::strtolower( $search );

		//$where = array();
		if ($search) {
			$where = 'WHERE nama_propinsi LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false );
		}


		$task = JRequest::getVar('task');
		$query_view = '
		SELECT * FROM #__propinsi '.$where;
		$query_edit =  '
		SELECT * FROM #__propinsi where propinsi_id = '.$this->_id . $where;

		//---------------------- get propinsi all
		if (!isset($task) || empty($task) || !empty( $this->_data )) {
			$this->_data = $this->_getList( $query_view, $this->getState('limitstart'), $this->getState('limit') );
			//echo $query_view;
			return $this->_data;
		}
		else {
		//---------------------- get propinsi all
			if ( empty($this->_data ) ){
				$this->_db->setQuery($query_edit);
				$this->_data = $this->_db->loadObject();
			}
			else {
				$this->_data = new stdClass();
				$this->_data->id = 0;
				$this->_data->name = null;
			}
			return $this->_data;
		}
	}

	// function to store data into database
	function store($post){
		$row =& JTable::getInstance('propinsi', 'Table');
		$data = JRequest::get( 'post' );
		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		// Store the web link table to the database
		if (!$row->store()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		return true;
	}

	// function to get total of data
	function getTotal(){
		global $mainframe,$option;
		$search	= $mainframe->getUserStateFromRequest( $option.'search','search','','string' );
		$search	= JString::strtolower( $search );

		//$where = array();
		if ($search) {
			$where = 'WHERE nama_propinsi LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false );
		}

		$query_view = '
		SELECT count(*) FROM #__propinsi ';
		//echo $query_view;
		$this->_db->setQuery($query_view);
		$this->_total = $this->_db->loadResult();
		//echo $this->_db->setQuery($query_view);

		if(!$this->_db->query()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		return $this->_total;
/*
		if (empty($this->_total)){
			$this->_total = $this->_getListCount( $query_view );
			echo $this->_total;
		}
		return $this->_total;
*/
	}

	// function to setup paging
	function getPagination(){
		if (empty($this->_pagination)){
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
		}
		return $this->_pagination;
	}
}
